﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;

namespace Camping_Project.Forms
{
    public partial class FormFacturation : Form
    {

        private int idReservation = 0;

        public FormFacturation()
        {
            InitializeComponent();
        }

        private void FormFacturation_Load(object sender, EventArgs e)
        {
            refreshDGVClient();
        }

        private void clearCells()
        {

            tbEmplacement.Text = "";
            tbAdultes.Text = "";
            tbEnfants.Text = "";
            tbAnimaux.Text = "";
            tbVehicules.Text = "";
            tbElectricite.Text = "";
            tbPrixBase.Text ="";
            tbPrixAdultes.Text = "";
            tbPrixEnfants.Text = "";
            tbPrixAnimaux.Text = "";
            tbPrixVehicules.Text = "";
            tbPrixElectricite.Text = "";
            tbPrixTotal.Text = "";
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            Camping_BDD bdd = new Camping_BDD();
            int ligne = dataGridView1.SelectedCells[0].RowIndex;
            int id = (int)dataGridView1["iDClientDataGridViewTextBoxColumn", ligne].Value;

            //Nettoyage des cellules
            clearCells();

            var query =
                from client in bdd.Client
                join reservation in bdd.Reservation on id equals reservation.IDClient
                where reservation.PayementEffectue == false
                select new
                {
                    reservation.IDReservation,
                    reservation.TypeEmp,
                    reservation.NbAdultes,
                    reservation.NbEnfants,
                    reservation.NbAnimaux,
                    reservation.NbVehicules,
                    reservation.Electricite,
                    reservation.PrixNuit,
                    reservation.PrixAdulte,
                    reservation.PrixEnfant,
                    reservation.PrixAnimal,
                    reservation.PrixVehicule,
                    reservation.PrixElectricite,
                    reservation.PrixTotal
                };


            foreach (var p in query)
            {
                if(p.IDReservation.Equals(null))
                {
                    MessageBox.Show("foo");
                }

                if (p.TypeEmp == false)
                    tbEmplacement.Text = "Tente";
                else
                    tbEmplacement.Text = "Caravane/Mobile home";
                tbAdultes.Text = p.NbAdultes.ToString();
                tbEnfants.Text = p.NbEnfants.ToString();
                tbAnimaux.Text = p.NbAnimaux.ToString();
                tbVehicules.Text = p.NbVehicules.ToString();
                if (p.Electricite == true)
                    tbElectricite.Text = "Oui";
                else
                    tbElectricite.Text = "Non";
                tbPrixBase.Text = p.PrixNuit.ToString();
                tbPrixAdultes.Text = p.PrixAdulte.ToString();
                tbPrixEnfants.Text = p.PrixEnfant.ToString();
                tbPrixAnimaux.Text = p.PrixAnimal.ToString();
                tbPrixVehicules.Text = p.PrixVehicule.ToString();
                tbPrixElectricite.Text = p.PrixElectricite.ToString();
                tbPrixTotal.Text = p.PrixTotal.ToString();

                idReservation = p.IDReservation;

            }
        }

        public void refreshDGVClient()
        {
            Camping_BDD bdd = new Camping_BDD();
            this.dataGridView1.DataSource = from c in bdd.Client select new { c.IDClient, c.Nom, c.Prenom, c.DateNaissance, c.Adresse, c.CodePostal, c.Ville, c.Pays, c.Telephone, c.Email };
        }

        private void button1_Click(object sender, EventArgs e)
        {
      
                Camping_BDD bdd = new Camping_BDD();
                var reserv = (from r in bdd.Reservation
                              where r.IDReservation == idReservation
                              select r).Single();

                if (reserv.PrixTotal != 0)
                {

                    var cli = (from c in bdd.Client where c.IDClient == reserv.IDClient select c).Single();

                    //Create Document class object and set its size to letter and give space left, right, Top, Bottom Margin
                    Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
                    PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(Path.GetDirectoryName(Application.ExecutablePath) + @"\Factures\Facture_" + reserv.IDReservation + ".pdf", FileMode.Create));
                    doc.Open();//Open Document to write
                    //Write some content


                    Paragraph entete = new Paragraph("Camping Soleil                                                                                                                            FACTURE\nLe camping select de Bredenne\n\nKon. Astridlaan 57\n8450\nBredene\nTéléphone : 0412/00.00.00   Fax : 0412/00.00.01");
                    entete.Alignment = Element.ALIGN_LEFT;
                    //entete.Font.SetStyle(Font.Bold);

                    Paragraph fact = new Paragraph("\n\nDATE: " + String.Format("{0:dd/MM/yyyy}", DateTime.Now) + "\nN° facture : " + reserv.IDReservation);
                    fact.Alignment = Element.ALIGN_RIGHT;


                    Paragraph destinataire = new Paragraph("Facturé à : \nNom: " + cli.Nom.Trim() + " " + cli.Prenom + "\nAdresse: " + cli.Adresse + "\n" + cli.CodePostal.Trim() + " " + cli.Ville.Trim() + ", " + cli.Pays);
                    destinataire.Alignment = Element.ALIGN_LEFT;


                    Paragraph paragraph = new Paragraph("Facture n°" + reserv.IDReservation);
                    Phrase phrase = new Phrase("Emplacement : " + reserv.IDEmplacement + "\nDate d'arrivée : " + String.Format("{0:dd/MM/yyyy}", reserv.DateDebut) + " - Date de départ : " + String.Format("{0:dd/MM/yyyy}", reserv.DateFinDef));
                    //Chunk chunk = new Chunk(" This is my third line using Chunk.");
                    //Tableau contenant les données


                    PdfPTable table = new PdfPTable(3);

                    PdfPCell cell = new PdfPCell(new Phrase("Details de la facture"));

                    cell.Colspan = 3;

                    cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right


                    //Description
                    table.AddCell("Description");

                    table.AddCell("Nombre");

                    table.AddCell("Prix");


                    //1
                    table.AddCell("Prix de base");

                    table.AddCell("/");

                    table.AddCell(reserv.PrixNuit + "");

                    //2
                    table.AddCell("Nombre d'adultes supplémentaires");

                    table.AddCell(reserv.NbAdultes + "");

                    table.AddCell(reserv.PrixAdulte + "");

                    //3

                    table.AddCell("Nombre d'enfants");

                    table.AddCell(reserv.NbEnfants + "");

                    table.AddCell(reserv.PrixEnfant + "");

                    //4

                    table.AddCell("Nombre d'animaux");

                    table.AddCell(reserv.NbAnimaux + "");

                    table.AddCell(reserv.PrixAnimal + "");

                    //5

                    table.AddCell("Nombre de vehicules");

                    table.AddCell(reserv.NbVehicules + "");

                    table.AddCell(reserv.PrixVehicule + "");

                    //6

                    table.AddCell("Raccordement Electrique");

                    table.AddCell(tbElectricite.Text + "");

                    table.AddCell(reserv.PrixElectricite + "");

                    //Total

                    table.AddCell("");

                    table.AddCell("TOTAL");

                    table.AddCell(reserv.PrixTotal + "");


                    //Ajout des éléments dans le document
                    doc.Add(entete);
                    doc.Add(fact);
                    doc.Add(destinataire);
                    doc.Add(paragraph);

                    doc.Add(phrase);

                    doc.Add(table);

                    doc.Close();


                    reserv.PayementEffectue = true;

                    MessageBox.Show("La facture a bien été crée dans le dossier " + Path.GetDirectoryName(Application.ExecutablePath) + @"\Factures\ au numero " + reserv.IDReservation, "Succes de l'opération", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    try
                    {
                        bdd.SaveChanges();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                        // Provide for exceptions.
                    }

                }
                else
                {
                    MessageBox.Show("Veuillez d'abord définir la date de départ définitive", "Echec de l'opération", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            
                
            


        }


     
    }
}
